let {
	getUserInfo
} = uni.$http.user

const state = {
	token: uni.getStorageSync('token') || '',
	refreshToken: uni.getStorageSync('refreshToken') || '',
	userInfo: uni.getStorageSync('userInfo') ? JSON.parse(uni.getStorageSync('userInfo')) : {},
}

const mutations = {
	SET_TOKEN: (state, data) => {
		if (data.token) {
			uni.setStorageSync('token', data.token)
			uni.setStorageSync('refreshToken', data.refreshToken)
			state.token = data.token
			state.refreshToken = data.refreshToken
		} else {
			uni.removeStorageSync('token')
			uni.removeStorageSync('refreshToken')
			state.token = ''
		}
	},
	SET_USERINFO: (state, userinfo) => {
		if (JSON.stringify(userinfo) !== '{}') {
			state.userInfo = userinfo
			uni.setStorageSync('userInfo', JSON.stringify(userinfo))
		} else {
			state.userInfo = userinfo
			uni.removeStorageSync('userInfo')
		}
	}
}

const actions = { // 登录
	login({
		commit,
		dispatch,
	}, playLoad) {
		const {
			accessToken,
		} = playLoad
		console.log(playLoad, 36);
		// 设置登录状态
		commit('SET_TOKEN', {
			token: accessToken,
		})
		// 初始化用户信息
		dispatch('initUserInfo')
	},
	// 退出
	logout({
		commit,
		dispatch
	}) {
		return new Promise((resolve, reject) => {
			// 设置登录状态
			commit('SET_TOKEN', {
				token: '',
				refreshToken: ''
			})
			// 设置用户信息
			commit('SET_USERINFO', {})
			resolve()
		})
	},
	// 初始化用户信息
	initUserInfo({
		commit,
		dispatch,
	}) {
		getUserInfo().then(result => {
			commit('SET_USERINFO', result.data)
		})
		// uni.$u.http.get('/personal/user/info', {
		// 	custom: {
		// 		auth: true
		// 	}
		// }).then(result => {

		// })
	}
}

export default {
	state,
	mutations,
	actions
}